package com.哈希表;



import java.util.LinkedList;

class MyBucket4{
        LinkedList<Integer> list ;
        public MyBucket4(){
            list = new LinkedList<>();
        }
        /**
         * 添加之前先清空
         * @param value
         */
        public void add(int value){
            if(list.size() != 0) {
                list.remove();
            }
            list.add(value);

        }
        public void remove(){
            if(list.size() != 0) {
                list.remove();
            }
        }

        /**
         * 没有就返回-1
         * @param
         * @return
         */
        public int get(){
            if(list.size()!=0){
                return list.getFirst();
            }
            return -1;
        }
    }
    public class MyHashMap4{
        int range = 2069;
        MyBucket4[] myBucket ;
        public MyHashMap4(){
            myBucket = new MyBucket4[range];
            for(int i = 0;i<myBucket.length;i++){
                myBucket[i] = new MyBucket4();
            }
        }
        public int _hase(int key){
            return key%2069;
        }
        /** value will always be non-negative. */
        public void put(int key, int value) {
            key = this._hase(key);
            myBucket[key].add(value);
        }

        /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
        public int get(int key) {
            key = this._hase(key);
            return myBucket[key].get();
        }

        /** Removes the mapping of the specified value key if this map contains a mapping for the key */
        public void remove(int key) {
            key = this._hase(key);
            myBucket[key].remove();
        }
    public String toString(){
        StringBuilder builder = new StringBuilder();
        for(int i = 0 ;i<myBucket.length;i++){
            builder.append(String.format("键:%s,桶{",i));
            myBucket[i].list.forEach(item->{
                builder.append(item).append(",");
            });
            builder.append("}").append("\n");

        }
        return builder.toString();
    }

    public static void main(String[] args) {
        MyHashMap4 myHashMap = new MyHashMap4();
        myHashMap.put(1,2);
        myHashMap.put(2,3);
        myHashMap.put( 3,4);
        myHashMap.put(1,3);
        myHashMap.remove(1);
        System.out.println(myHashMap.toString());
        System.out.println(myHashMap.get(2));
        System.out.println(myHashMap.get(1));


    }
}
